﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

namespace AppEszköz.UserManagement
{
    public class UserManager
    {
        public static string SqlConnectionString { get; set; }

        public UserManager()
        {
        }
        public User GetUser(string username)
        {
            if (UserManager.SqlConnectionString == null)
                throw new ArgumentNullException("SqlConnectionString", "SqlConnectionString cannot be null");
            User user = new User();
            user.UserName = username.ToLower();
            user.Login.Username = user.UserName;
            user.Login.VerifyUsername();
            if (!user.Login.UsernameValid)
                return (User)null;
            SqlDataAdapter sqlDataAdapter = (SqlDataAdapter)null;
            SqlConnection sqlConnection = (SqlConnection)null;
            DataSet dataSet = new DataSet();
            try
            {  
                //SqlParameterBuilder parameterBuilder = new SqlParameterBuilder();
                //sqlConnection = new SqlConnection(UserManager.SqlConnectionString);
                //SqlCommand selectCommand = new SqlCommand();
                //selectCommand.Connection = sqlConnection;
                //selectCommand.CommandType = CommandType.StoredProcedure;
                //selectCommand.CommandText = "spUserAccessRoles";
                //selectCommand.Parameters.Add(parameterBuilder.CreateParameter("@Username", SqlDbType.VarChar, (object)user.UserName));
                //sqlDataAdapter = new SqlDataAdapter(selectCommand);
                //sqlDataAdapter.Fill(dataSet);
                //if (dataSet.Tables[0].Rows.Count > 0)
                //{
                //    user.FirstName = user.Login.GivenName;
                //    user.LastName = user.Login.Surname;
                //    user.Email = user.Login.Email;
                //    user.UserId = (int)dataSet.Tables[0].Rows[0]["UserID"];
                //    user.Status = (UserStatus)Enum.Parse(typeof(UserStatus), (string)dataSet.Tables[0].Rows[0]["Status"], true);
                //}
                //user.AccessRoles = new AccessRole[dataSet.Tables[1].Rows.Count];
                //for (int index = 0; index < dataSet.Tables[1].Rows.Count; ++index)
                //{
                //    user.AccessRoles.Insert(index, new AccessRole());
                //    user.AccessRoles.ElementAt(index).RoleID = (int)dataSet.Tables[1].Rows[index]["RoleID"];
                //    user.AccessRoles.ElementAt(index).RoleName = dataSet.Tables[1].Rows[index]["RoleName"].ToString();
                //}
            }
            finally
            {
                if (sqlDataAdapter != null)
                    sqlDataAdapter.Dispose();
                if (sqlConnection != null && sqlConnection.State != ConnectionState.Closed)
                    sqlConnection.Close();
            }
            return user;
        }
    }
}